/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.gateway.configurer.exception;

import com.arisgo.cloud.common.Result;
import com.arisgo.cloud.common.exception.CloudException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;

/**
 * TODO 异常处理
 *
 * @author Arisgo
 * @since 2024/6/4
 */
//@RestControllerAdvice
public class CloudExceptionHandle {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 业务异常
     *
     * @param e 异常
     */
    @ResponseStatus(BAD_REQUEST)
    @ExceptionHandler(CloudException.class)
    public Result<?> handleCloudException(CloudException e) {
        logger.error(e.getMessage(), e);
        return Result.fail(BAD_REQUEST.value(), e.getMessage());
    }

    /**
     * 全局异常
     *
     * @param e 异常
     */
    @ResponseStatus(INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public Result<?> handleException(Exception e) {
        logger.error(e.getMessage(), e);
        return Result.fail(INTERNAL_SERVER_ERROR.value(), e.getMessage());
    }

}
